/*----------------------------------------------------------------------------------------------------------------------#
#-----------------------------------------------------------------------------------------------------------------------#
#------  This File is Part Of : ----------------------------------------------------------------------------------------#
#------- _  -------------------  ______   _   --------------------------------------------------------------------------#
#------ | | ------------------- (_____ \ | |  --------------------------------------------------------------------------#
#------ | | ---  _   _   ____    _____) )| |  ____  _   _   ____   ____   ----------------------------------------------#
#------ | | --- | | | | / _  |  |  ____/ | | / _  || | | | / _  ) / ___)  ----------------------------------------------#
#------ | |_____| |_| |( ( | |  | |      | |( ( | || |_| |( (/ / | |  --------------------------------------------------#
#------ |_______)\____| \_||_|  |_|      |_| \_||_| \__  | \____)|_|  --------------------------------------------------#
#------------------------------------------------- (____/  -------------------------------------------------------------#
#------------------------   ______   _   -------------------------------------------------------------------------------#
#------------------------  (_____ \ | |  -------------------------------------------------------------------------------#
#------------------------   _____) )| | _   _   ___   ------------------------------------------------------------------#
#------------------------  |  ____/ | || | | | /___)  ------------------------------------------------------------------#
#------------------------  | |      | || |_| ||___ |  ------------------------------------------------------------------#
#------------------------  |_|      |_| \____|(___/   ------------------------------------------------------------------#
#-----------------------------------------------------------------------------------------------------------------------#
#-----------------------------------------------------------------------------------------------------------------------#
#- Licensed under the GPL License --------------------------------------------------------------------------------------#
#-----------------------------------------------------------------------------------------------------------------------#
#- Copyright (c) Nanni <cusunanni@hotmail.it> --------------------------------------------------------------------------#
#- Copyright (c) Rinnegatamante <rinnegatamante@eternalongju2.com> -----------------------------------------------------#
#-----------------------------------------------------------------------------------------------------------------------#
#- Official Forum : http://rinnegatamante.eu/luaplayerplus/forum.php ---------------------------------------------------#
#- For help using LuaPlayerPlus, coding help, and other please visit : http://rinnegatamante.eu/luaplayerplus/forum.php #
#-----------------------------------------------------------------------------------------------------------------------#
#- Credits : -----------------------------------------------------------------------------------------------------------#
#-----------------------------------------------------------------------------------------------------------------------#
#- Homemister for LPHM sourcecode --------------------------------------------------------------------------------------#
#- Zack & Shine for LP Euphoria sourcecode -----------------------------------------------------------------------------#
#- ab5000 for support on psp-ita.com -----------------------------------------------------------------------------------#
#- valantin for sceIoMvdir and sceIoCpdir improved functions------------------------------------------------------------#
#- Dark_AleX for usbdevice ---------------------------------------------------------------------------------------------#
#- VirtuosFlame & ColdBird for iso drivers and kuBridge ----------------------------------------------------------------#
#- sakya for Media Engine and OslibMod ---------------------------------------------------------------------------------#
#- Booster & silverspring for EEPROM write/read functions --------------------------------------------------------------#
#- Akind for RemoteJoyLite ---------------------------------------------------------------------------------------------#
#- cooleyes for mpeg4 lib ----------------------------------------------------------------------------------------------#
#- Arshia001 for PSPAALib ----------------------------------------------------------------------------------------------#
#- InsertWittyName & MK2k for PGE sourcecode ---------------------------------------------------------------------------#
#- Youresam for LUA BMPLib ---------------------------------------------------------------------------------------------#
#- Raphael for vram manager code ---------------------------------------------------------------------------------------#
#- Dynodzzo for LSD concepts -------------------------------------------------------------------------------------------#
#- ab_portugal for Image.negative function -----------------------------------------------------------------------------#
#- JiCé for drawCircle function ----------------------------------------------------------------------------------------#
#- Rapper_skull & DarkGiovy for testing LuaPlayer Plus and coming up with some neat ideas for it. ----------------------#
#-----------------------------------------------------------------------------------------------------------------------#
#-----------------------------------------------------------------------------------------------------------------------#
#-----------------------------------------------------------------------------------------------------------------------#
#-----------------------------------------------------------------------------------------------------------------------*/

#include "Camera.h"
#include <pspusbcam.h>

static int luaCamera_init(lua_State *L)
{
    int ret = LPP_CamStartAll();
    lua_pushboolean(L, !(ret < 0));
    return 1;
}

static int luaCamera_initVideo(lua_State *L)
{
    Int32 argc = lua_gettop(L);
    if(argc > 1)
    {
        return luaL_error(L, "Camera.initVideo([Resolution]) takes a maximum of 1 argument.");
    }
    int ret = LPP_CamInitVideo(argc == 1? luaL_checkint(L,1) : PSP_USBCAM_RESOLUTION_480_272);
    lua_pushboolean(L, !(ret < 0));
    return 1;
}

static int luaCamera_shutdown(lua_State *L)
{
    int ret = LPP_CamStopAll();
    lua_pushboolean(L, !(ret < 0));
    return 1;
}

static int luaCamera_takePhoto(lua_State *L)
{
    Int32 argc = lua_gettop(L);
    if(argc != 2 && argc != 3)
    {
        return luaL_error(L, "Camera.takePhoto(filepath, compressionLvl, [resolution]) takes a maximum of 3 arguments.");
    }
    lua_pushboolean(L, LPP_CamTakePhoto(luaL_checkstring(L,1),luaL_checkint(L,2), argc >= 3 ? luaL_checkint(L, 3) : PSP_USBCAM_RESOLUTION_480_272) >= 0);
    return 1;
}

static int luaCamera_render(lua_State *L)
{
    lua_pushboolean(L, LPP_CamRenderScreen() >= 0);
    return 1;
}

static L_CONST luaL_reg luaCamera_methods[] = {
    { "init", luaCamera_init },
    { "initVideo", luaCamera_initVideo },
    { "render", luaCamera_render },
    { "shutdown", luaCamera_shutdown },
    { "takePhoto", luaCamera_takePhoto },
    { 0, 0 }
};

void luaCamera_Init(lua_State *L)
{
    luaL_openlib(L, "Camera", luaCamera_methods, 0);
	
    #define CAM_CONSTANT(name)\
    lua_pushstring(L, #name);\
    lua_pushnumber(L, PSP_USBCAM_##name);\
    lua_settable(L, -3);

    CAM_CONSTANT(RESOLUTION_160_120)
    CAM_CONSTANT(RESOLUTION_176_144)
    CAM_CONSTANT(RESOLUTION_320_240)
    CAM_CONSTANT(RESOLUTION_352_288)
    CAM_CONSTANT(RESOLUTION_640_480)
    CAM_CONSTANT(RESOLUTION_1024_768)
    CAM_CONSTANT(RESOLUTION_1280_960)
    CAM_CONSTANT(RESOLUTION_480_272)
    CAM_CONSTANT(RESOLUTION_360_272)
}
